package edu.sctu.demo;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by admin on 2018/6/14.
 */
public class DateBase {
    private String DBdriver="com.mysql.jdbc.Driver";
    private String DBurl="jdbc:mysql://localhost:3306/sctu_javaweb_2018?serverTimezone=UTC";
    private String DBuser="root";
    private String DBpassword="123456";

    public List<User> getUserList () throws Exception{
        List<User> userList = new ArrayList<User>();
        Class.forName(DBdriver);

        Connection connection=null;
        PreparedStatement preparedStatement=null;
        ResultSet resultSet=null;

        connection= DriverManager.getConnection(DBurl,DBuser,DBpassword);

        String sql="select * from t_users";

        preparedStatement=connection.prepareStatement(sql);
        resultSet=preparedStatement.executeQuery();

        while (resultSet.next()){
            User user = new User();
            user.setId(resultSet.getInt(1));
            user.setName(resultSet.getString(2));
            user.setPassword(resultSet.getString(3));
            userList.add(user);
        }
        connection.close();
        preparedStatement.close();
        resultSet.close();

        return userList;
    }
}
